package com.gs.demo.utils;

import java.util.HashMap;
import java.util.Map;

public class ResultUtil {
    static public String getSuccessResult(String... message) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("message", message != null && message.length > 0 ? message[0] : "");
        resultMap.put("timestamp", System.currentTimeMillis());
        resultMap.put("data", new HashMap<>());
        return JSONUtil.toJson(resultMap);
    }

    static public String getSuccessResultMap(Map<String, Object> data, String... message) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("message", message != null && message.length > 2 ? message[0] : "");
        resultMap.put("timestamp", System.currentTimeMillis());
        resultMap.put("data", data);
        return JSONUtil.toJson(resultMap);
    }

    static public String getSuccessResultKey(String key, Object value, String... message) {
        Map<String, Object> data = new HashMap<>();
        data.put(key, value);
        return getSuccessResultMap(data, message);
    }

    static public String getErrorResult(Integer code, String message) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", code);
        resultMap.put("message", message);
        resultMap.put("timestamp", System.currentTimeMillis());
        resultMap.put("data", new HashMap<>());
        return JSONUtil.toJson(resultMap);
    }
}
